home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / NEW - Update Notifier / Bin / update_notifier-0.1.1-fx+tb.xpi / components / nsUNManager.js < prev    next >
Encoding:
Text File  |  2006-02-21  |  8.4 KB  |  294 lines

  1. // Update Notifier
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/updatenotifier/
  4.  
  5. const CC = Components.classes;
  6. const CI = Components.interfaces;
  7.  
  8. const ITEM = "urn:mozilla:item:";
  9.  
  10. const NOTIFY_UPDATE_CHANGE = "UN:update-change";
  11.  
  12. function UNManager() {}
  13. UNManager.prototype = {
  14.   _items: new Array(),
  15.   _firstTime: true,
  16.   _check: false,
  17.   _install: false,
  18.   
  19.   set check(aCheck) { this._check = aCheck; },
  20.   set install(aInstall) { this._install = aInstall; },
  21.   
  22.   getNumItems: function()
  23.   {
  24.     return this._items ? this._items.length : 0;
  25.   },
  26.   
  27.   getNumItemsByType: function(aType)
  28.   {
  29.     var num = 0;
  30.     
  31.     for (var i = 0; i < this._items.length; i++)
  32.       if (this._items[i].type == aType)
  33.         num++;
  34.     
  35.     return num;
  36.   },
  37.   
  38.   getItemByIndex: function(aNum)
  39.   {
  40.     return this._items ? this._items[aNum] : null;
  41.   },
  42.   
  43.   checkUpdates: function()
  44.   {
  45.     var items = this._em.getItemList(CI.nsIUpdateItem.TYPE_ANY, {});
  46.     this._em.update(items, items.length, false, null);
  47.   },
  48.   
  49.   installUpdates: function()
  50.   {
  51.     var itemList = new Array();
  52.     
  53.     for (var i = 0; i < this._items.length; i++)
  54.       itemList.push(this._em.getItemForID(this._items[i].id));
  55.     
  56.     if (itemList.length > 0)
  57.       this._em.addDownloads(itemList, itemList.length, true);
  58.   },
  59.   
  60.   load: function()
  61.   {
  62.     if (this._firstTime)
  63.     {
  64.       this._firstTime = false;
  65.       
  66.       this._em = CC["@mozilla.org/extensions/manager;1"].getService(CI.nsIExtensionManager);
  67.       this._cs = CC['@mozilla.org/consoleservice;1'].getService(CI.nsIConsoleService);
  68.       this._observer = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
  69.       
  70.       // Adds observer for item updates
  71.       this._em.datasource.AddObserver(this);
  72.       
  73.       if (this._check)
  74.         this.checkUpdates();
  75.     }
  76.     
  77.     // Checks the RDF for updates
  78.     this._loadItemUpdates();
  79.   },
  80.   
  81.   _loadItemUpdates: function()
  82.   {
  83.     var rs = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
  84.     var cu = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils);
  85.     var ds = this._em.datasource;
  86.     
  87.     var res = rs.GetResource(ITEM+"root");
  88.     var container = null;
  89.     
  90.     if(cu.IsSeq(ds, res)) {
  91.       container = CC["@mozilla.org/rdf/container;1"].getService(CI.nsIRDFContainer);
  92.       container.Init(ds, res); 
  93.     }
  94.     else
  95.       container = cu.MakeSeq(ds, res);
  96.     
  97.     // Gets the datasource resources
  98.     var elements = container.GetElements();
  99.     
  100.     while (elements.hasMoreElements()) {
  101.       var element = elements.getNext().QueryInterface(CI.nsIRDFResource); 
  102.       var item = this._getItemUpdateInfo(this._em.datasource, element);
  103.       
  104.       if (item.url != null && item.version != null)
  105.         this._addItemUpdate(item);
  106.     }
  107.   },
  108.   
  109.   _getItemUpdateInfo: function(aDataSource, aSource)
  110.   {
  111.     var arcCursor = aDataSource.ArcLabelsOut(aSource);
  112.     var item = new Object();
  113.     
  114.     item.id = aSource.Value.replace(ITEM, "");
  115.     item.url = null;
  116.     item.version = null;
  117.     
  118.     while(arcCursor.hasMoreElements())
  119.     {
  120.       thisArc = arcCursor.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  121.       
  122.       var target = aDataSource.GetTarget(aSource, thisArc, true);
  123.       var rdfLiteral = null;
  124.       
  125.       if (target instanceof CI.nsIRDFLiteral)
  126.         rdfLiteral = target.Value;
  127.       
  128.       if (thisArc.Value.indexOf("availableUpdateURL") > -1)
  129.         item.url = rdfLiteral;
  130.       else if (thisArc.Value.indexOf("availableUpdateVersion") > -1)
  131.         item.version = rdfLiteral;
  132.     }
  133.     
  134.     return item;
  135.   },
  136.   
  137.   _addItemUpdate: function(aItem)
  138.   {
  139.     // Double check for valid item
  140.     if (aItem && aItem.url != null && aItem.version != null)
  141.     {
  142.       var item = this._em.getItemForID(aItem.id);
  143.       var uItem = CC["@longfocus.com/updatenotifier/item;1"].createInstance(CI.nsIUNItem);
  144.       
  145.       // Update item
  146.       uItem.id = aItem.id;
  147.       uItem.type = (item.type == 2 ? "extension" : "theme");
  148.       uItem.url = aItem.url;
  149.       uItem.version = aItem.version;
  150.       
  151.       /*
  152.       this._cs.logStringMessage("--- New Update ---" + "\n" +
  153.                                 uItem.id + "\n" + 
  154.                                 uItem.type + "\n" + 
  155.                                 uItem.url + "\n" + 
  156.                                 uItem.version + "\n" + 
  157.                                 "---");
  158.       */
  159.       
  160.       var index = this._getItemIndex(uItem);
  161.       var updated = false;
  162.       
  163.       if (index > -1) {
  164.         var item = this._items[index];
  165.         
  166.         if (item.url != uItem.url || item.version != uItem.version) {
  167.           this._items[index] = uItem;
  168.           updated = true;
  169.         }
  170.       }
  171.       else {
  172.          this._items[this._items.length] = uItem;
  173.          updated = true;
  174.       }
  175.       
  176.       // Check for updated item
  177.       if (updated)
  178.       {
  179.         // Check for install
  180.         if (this._install)
  181.           // Installs updates
  182.           this.installUpdates();
  183.         else
  184.           // Notify of update change
  185.           this._observer.notifyObservers(
  186.                            uItem, NOTIFY_UPDATE_CHANGE,
  187.                            this.getNumItemsByType(uItem.type));
  188.       }
  189.     }
  190.   },
  191.   
  192.   _removeItemUpdate: function(aItem)
  193.   {
  194.     var index = this._getItemIndex(aItem);
  195.     
  196.     if (index > -1)
  197.     {
  198.       var item = this._items[index];
  199.       this._items.splice(index, 1);
  200.       
  201.       // Notify for removed update
  202.       this._observer.notifyObservers(
  203.                        item, NOTIFY_UPDATE_CHANGE,
  204.                        this.getNumItemsByType(item.type));
  205.     }
  206.   },
  207.   
  208.   _getItemIndex: function(aItem)
  209.   {
  210.     var index = -1;
  211.     
  212.     for (var i = 0; i < this._items.length && index == -1; i++)
  213.       if (this._items[i].id == aItem.id)
  214.         index = i;
  215.     
  216.     return index;
  217.   },
  218.   
  219.   onChange: function(aDataSource, aSource, aProperty, aOldTarget, aNewTarget)
  220.   {
  221.     var pv = aProperty.QueryInterface(CI.nsIRDFResource).Value;
  222.     
  223.     if (pv.indexOf("availableUpdateURL") > -1 || pv.indexOf("availableUpdateVersion") > -1) {
  224.       var item = this._getItemUpdateInfo(aDataSource, aSource);
  225.       this._addItemUpdate(item);
  226.     }
  227.   },
  228.   
  229.   onUnassert: function(aDataSource, aSource, aProperty, aTarget)
  230.   {
  231.     var pv = aProperty.QueryInterface(CI.nsIRDFResource).Value;
  232.     
  233.     if (pv.indexOf("availableUpdateURL") > -1 || pv.indexOf("availableUpdateVersion") > -1) {
  234.       var item = this._getItemUpdateInfo(aDataSource, aSource);
  235.       this._removeItemUpdate(item);
  236.     }
  237.   },
  238.   
  239.   onAssert: function(aDataSource, aSource, aProperty, aTarget) {},
  240.   onBeginUpdateBatch: function(aDataSource) {},
  241.   onEndUpdateBatch: function(aDataSource) {},
  242.   onMove: function(aDataSource, aOldSource, aNewSource, aProperty, aTarget) {},
  243.   
  244.   QueryInterface: function(iid)
  245.   {
  246.     if (!iid.equals(Components.interfaces.nsIUNManager) &&
  247.         !iid.equals(Components.interfaces.nsISupports))
  248.       throw Components.results.NS_ERROR_NO_INTERFACE;
  249.     return this;
  250.   }
  251. }
  252.  
  253. var myModule = {
  254.   firstTime: true,
  255.   
  256.   myCID: Components.ID("{0090c2b0-9e45-11da-a746-0800200c9a66}"),
  257.   myDesc: "Manager for new available updates",
  258.   myProgID: "@longfocus.com/updatenotifier/manager;1",
  259.   myFactory: {
  260.     createInstance: function (outer, iid) {
  261.       if (outer != null)
  262.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  263.       
  264.       return (new UNManager()).QueryInterface(iid);
  265.     }
  266.   },
  267.  
  268.   registerSelf: function (compMgr, fileSpec, location, type)
  269.   {
  270.     if (this.firstTime) {
  271.       this.firstTime = false;
  272.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  273.     }
  274.     
  275.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  276.     compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
  277.   },
  278.  
  279.   getClassObject: function (compMgr, cid, iid)
  280.   {
  281.     if (!cid.equals(this.myCID))
  282.       throw Components.results.NS_ERROR_NO_INTERFACE;
  283.     
  284.     if (!iid.equals(Components.interfaces.nsIFactory))
  285.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  286.     
  287.     return this.myFactory;
  288.   },
  289.   
  290.   canUnload: function(compMgr) { return true; }
  291. };
  292.  
  293. function NSGetModule(compMgr, fileSpec) { return myModule; }
  294.